package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PurchaseStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author atguigu-mqx
 * @ClassName VipPurchaseStrategy
 * @description: TODO
 * @date 2024年04月29日
 * @version: 1.0
 */
@Service("1003")
@Slf4j
public class VipPurchaseStrategy implements PurchaseStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void purchase(UserPaidRecordVo userPaidRecordVo) {
//	购买vip user_vip_service ，不需要去重注意订单No-校验；因为vip 可以续期.
        Long count = this.userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo()));
        if (count>0){
            return;
        }
        //  1.保存记录 user_vip_service 2.更新 user_info.is_vip=1, user_info.vip_expire_time

        //  获取用户购买vip信息对象.
        Long vipServiceConfigId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceConfigId);
        //  购买的时长.
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //  获取到用户基本信息.
        UserInfo userinfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());

        //  创建对象
        UserVipService userVipService = new UserVipService();
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        //  当前创建时间.
        userVipService.setStartTime(new Date());
        //  当前系统时间
        Date currentTime = new Date();
        //  判断 续期
        if (userinfo.getIsVip() == 1 && userinfo.getVipExpireTime().after(new Date())) {
            //  月份累加：
            currentTime = new LocalDateTime(userinfo.getVipExpireTime()).toDate();
        }
        //  月份相加：
        LocalDateTime localDateTime = new LocalDateTime(currentTime).plusMonths(serviceMonth);
        userVipService.setExpireTime(localDateTime.toDate());
        //  保存
        userVipServiceMapper.insert(userVipService);
        //  int i = 1/0;
        //  赋值：
        userinfo.setIsVip(1);
        userinfo.setVipExpireTime(localDateTime.toDate());
        userInfoMapper.updateById(userinfo);
    }
}
